package cn.lyq.card.ui.order

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.OrderDetailInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.utils.loadUrl
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_order_detail.*
import kotlinx.android.synthetic.main.item_device.*
import kotlinx.android.synthetic.main.layout_title.*


class OrderDetailActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(OrderVM::class.java)
    }
    private val mInfo by lazy {
        intent.getSerializableExtra(KEY_INFO)
            ?.takeIf { it is OrderDetailInfo }
            ?.run { this as OrderDetailInfo }
    }

    companion object {
        private const val KEY_INFO = "INFO"

        @JvmStatic
        fun start(context: Context, info: OrderDetailInfo) {
            val starter = Intent(context, OrderDetailActivity::class.java)
                .putExtra(KEY_INFO, info)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.act_order_detail
    }

    override fun initView() {
        super.initView()
        itemDevice.setBackgroundColor(ContextCompat.getColor(this, R.color.bg_white))
        itemDevice.elevation = 0f
        tvBuyDevice.visibility = View.GONE

        setMyTitle(R.string.str_order_detail)
        tvTitleSplit.visibility = View.GONE
    }

    override fun initListener() {
        super.initListener()
        tvCopy.setClick {
            val orderNo = tvOrderNo.text.toString()
            if (orderNo.isNotEmpty()) {
                ClipboardUtils.copyText(orderNo)
                ToastUtils.showShort(R.string.str_copy_success)
            }
        }
    }

    override fun initObserver() {
        super.initObserver()
        mInfo?.let {
            setData(it)
        }
        getDetail()
    }

    private fun getDetail() {
        mModel.getOrderDetail(mInfo?.id.toString()).observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> showLoading()
                Status.SUCCESS -> {
                    setData(it.requireData())
                    dismissLoading()
                }
                Status.ERROR -> {
                    dismissLoading()
                    ToastUtils.showShort(it.message.getMsg())
                }
            }
        })
    }

    private fun setData(info: OrderDetailInfo) {
        tvUserName.text = info.trueName ?: ""
        tvPhone.text = info.mobile ?: ""
        tvAddress.text = "${info.address ?: ""} ${info.detailsAddress ?: ""}"
        imgDevice.loadUrl(info.img)
        tvName.text = "机具类型：${info.goodsName ?: ""}"
        tvPrice.text = "机具价格：${info.goodsPrice ?: ""}元/台"

        tvNum.text = if (info.number == 0) "" else info.number.toString()
        val money = info.goodsPrice?.toDoubleOrNull() ?: 0.0 * info.number
        if (money != 0.0) {
            tvAmount.text = "￥$money"
        }
        tvRemark.text = info.remark

        tvOrderNo.text = info.orderNumber ?: ""
        tvPayTime.text = info.payTime ?: ""
        tvPayType.text = info.payName ?: ""

    }
}